package cn.cuiqi.dynamic.thread.pool.trigger;


import cn.cuiqi.dynamic.thread.pool.sdk.domain.model.Entity.ThreadPoolConfigEntity;
import cn.cuiqi.dynamic.thread.pool.types.Response;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RList;
import org.redisson.api.RTopic;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: Cuiqi
 * @Description:
 * @Date: 2024/8/2 21:39
 **/
@Slf4j
@CrossOrigin("*")
@RestController
@RequestMapping("/api/v1/dynamic/thread/pool")
public class DynamicThreadPoolController {
    
    
    
    
    @Autowired
    private RedissonClient redissonClient;

    /**
     * 查询线程池集合
     * @return
     */
    @GetMapping("/query_thread_pool_list")
    public Response<List<ThreadPoolConfigEntity>> queryThreadPoolList() {
        try {
            RList<ThreadPoolConfigEntity> cacheList = redissonClient.getList("THREAD_POOL_CONFIG_LIST_KEY");
//            log.info("线程池集合：{}", JSON.toJSONString(cacheList));
            return Response.<List<ThreadPoolConfigEntity>>builder()
                    .code(Response.Code.SUCCESS.getCode())
                    .info(Response.Code.SUCCESS.getInfo())
                    .data(cacheList.readAll())
                    .build();
        } catch (Exception e) {
            log.error("查询线程池数据异常", e);
            return Response.<List<ThreadPoolConfigEntity>>builder()
                    .code(Response.Code.UN_ERROR.getCode())
                    .info(Response.Code.UN_ERROR.getInfo())
                    .build();

        }
        
    }


    /**
     * 查询线程池配置
     * @return
     */
    @GetMapping("/query_thread_pool_config")
    public Response<ThreadPoolConfigEntity> queryThreadPoolConfig(@RequestParam String appName, @RequestParam String threadPoolName) {
        try {
            String cacheKey = "THREAD_POOL_CONFIG_PARAMETER_LIST_KEY" + "_" + appName + "_" + threadPoolName;
            ThreadPoolConfigEntity threadPoolConfigEntity =  redissonClient.<ThreadPoolConfigEntity>getBucket(cacheKey).get();
            log.info("线程池配置：{}", JSON.toJSONString(threadPoolConfigEntity));
            return Response.<ThreadPoolConfigEntity>builder()
                    .code(Response.Code.SUCCESS.getCode())
                    .info(Response.Code.SUCCESS.getInfo())
                    .data(threadPoolConfigEntity)
                    .build();
        } catch (Exception e) {
            log.error("查询线程池数据异常", e);
            return Response.<ThreadPoolConfigEntity>builder()
                    .code(Response.Code.UN_ERROR.getCode())
                    .info(Response.Code.UN_ERROR.getInfo())
                    .build();

        }

    }

    /**
     * 修改线程池参数
     * @param request
     * @return
     */
    @PostMapping("/update_thread_pool_config")
    public Response<Boolean> updateThreadPoolConfig(@RequestBody ThreadPoolConfigEntity request) {

        try {
            log.info("修改线程池配置开始{} {} {}", request.getAppName(), request.getThreadPoolName(), JSON.toJSON(request));
            String cacheKey = "DYNAMIC_THREAD_POOL_REDIS_TOPIC" + "_"  + request.getAppName();
            RTopic topic = redissonClient.getTopic(cacheKey);
            topic.publish(request);
            log.info("修改线程池配置完成 {} {}", request.getAppName(), request.getThreadPoolName());
            return Response.<Boolean>builder()
                    .code(Response.Code.SUCCESS.getCode())
                    .info(Response.Code.SUCCESS.getInfo())
                    .data(true)
                    .build();
        } catch (Exception e) {
            log.error("修改线程池配置异常 {}", JSON.toJSONString(request), e);
            return Response.<Boolean>builder()
                    .code(Response.Code.UN_ERROR.getCode())
                    .info(Response.Code.UN_ERROR.getInfo())
                    .data(false)
                    .build();

        }
    }
    
    
    
}
